package com.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DelFile {

    public static void scanDir(File rootDir, String token, List<File> list) {
        File[] files = rootDir.listFiles();
        if(files == null || files.length == 0) {
            return;
        }
        for(File file: files) {
            if(file.isDirectory()) {
                scanDir(file,token,list);
            } else {
                if(file.getName().contains(token)) {
                    list.add(file.getAbsoluteFile());
                }
            }
        }
    }
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的根目录（绝对或相对路径）：");
        String rootPath = scanner.next();
        File rootDir = new File(rootPath);
        if(!rootDir.exists()) {
            System.out.println("输入根目录不存在");
        }
        if(!rootDir.isDirectory()) {
            System.out.println("输入根目录不是目录");
        }
        System.out.println("请输入要查找的文件名中的字符：");
        String token = scanner.next();
        List<File> files = new ArrayList<>();
        scanDir(rootDir, token, files);
        System.out.println("共查找到符合的文件数量：" + files.size());
        for (File file: files) {
            System.out.println(file.getCanonicalPath()+" 是否删除？y/n");
            String opt = scanner.next();
            if(opt.toLowerCase().equals("y")) {
                file.delete();
            }
        }
    }
}
